﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using GameProtocol;
using UnityMultiPlayerGameServer.Clients;
using UnityMultiPlayerGameServer.Data;

namespace UnityMultiPlayerGameServer.Manager
{
    /// <summary>
    /// 游戏中玩家信息同步
    /// </summary>
    internal class GameSyncController : BaseController
    {
        public GameSyncController()
        {
            messageType = MessageType.GameSync;
        }


        public void ActionCharacterStateSync(Client client, GamePack pack)
        {
            //Console.WriteLine(pack.TestString);
            if (pack == null || pack.UserInfo.AccountID == null || pack.RoomInfo.RoomName == null 
                || pack.Position == null || pack.Rotation == null || pack.State == null)
            {
                Console.WriteLine("ActionCharacterStateSync : invalid pack");
                return;
            }

            //Console.WriteLine("client " + client != null);
            //Console.WriteLine(client.RemoteIP + " position  x:" + pack.Position.X + " y:" + pack.Position.Y + " z:" + pack.Position.Z);
            //Console.WriteLine(client.RemoteIP + " rotation  x:" + pack.Rotation.X + " y:" + pack.Rotation.Y + " z:" + pack.Rotation.Z);

            Room room = RoomManager.Instance.TryGetRoomByRoomname(pack.RoomInfo.RoomName);
            room?.BroadcastMessageForAllClient_UDP(pack);
        }

        public void ActionCharacterFire(Client client, GamePack pack)
        {
            if (pack == null || pack.UserInfo.AccountID == null || pack.RoomInfo.RoomName == null)
            {
                Console.WriteLine("ActionCharacterFire : invalid pack");
                return;
            }

            Console.WriteLine(client.RemoteIP + ":  fire_request");

            Room room = RoomManager.Instance.TryGetRoomByRoomname(pack.RoomInfo.RoomName);
            room?.BroadcastMessageForAllClient_UDP(pack);
        }

        public void ActionCharacterReload(Client client, GamePack pack)
        {
            if (pack == null || pack.UserInfo.AccountID == null || pack.RoomInfo.RoomName == null)
            {
                Console.WriteLine("ActionCharacterReload : invalid pack");
                return;
            }

            Console.WriteLine(client.RemoteIP + ":  reload_request");

            Room room = RoomManager.Instance.TryGetRoomByRoomname(pack.RoomInfo.RoomName);
            room?.BroadcastMessageForAllClient_UDP(pack);

        }

        public void ActionCharacterGetHurt(Client client, GamePack pack)
        {
            if (pack == null || pack.UserInfo.AccountID == null || pack.RoomInfo.RoomName == null)
            {
                Console.WriteLine("ActionCharacterGetHurt : invalid pack");
                return;
            }

            Console.WriteLine(client.RemoteIP + ":  hurt_request");

            Room room = RoomManager.Instance.TryGetRoomByRoomname(pack.RoomInfo.RoomName);
            room?.BroadcastMessageForAllClient_UDP(pack);

        }

        public void ActionCharacterDead(Client client, GamePack pack)
        {
            if (pack == null || pack.UserInfo.AccountID == null || pack.RoomInfo.RoomName == null)
            {
                Console.WriteLine("ActionCharacterDead : invalid pack");
                return;
            }

            Console.WriteLine(client.RemoteIP + ":  dead_request");

            Room room = RoomManager.Instance.TryGetRoomByRoomname(pack.RoomInfo.RoomName);
            room?.BroadcastMessageForAllClient_UDP(pack);
        }
    }
}
